/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.server;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import org.gwtportlets.portlet.server.PageRequest;
import org.gwtportlets.portlet.smartgwt.server.SmartWidgetDataProvider;
import za.org.foobar.famon.frontend.client.ui.LoginPortlet;

/**
 *
 */
public class LoginDataProvider  extends SmartWidgetDataProvider<LoginPortlet.Factory> {

    @Override
    public void refresh(LoginPortlet.Factory f, PageRequest req) {
        super.refresh(f, req);
        if (f.url != null) {
            UserService userService = UserServiceFactory.getUserService();
            if (userService.isUserLoggedIn()) {
                f.url = userService.createLogoutURL(f.url);
            } else {
                f.url = userService.createLoginURL(f.url);
            }

        }
    }

    public Class getWidgetFactoryClass() {
        return LoginPortlet.Factory.class;
    }
}
